////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRayHUD extends UTCTFHUD;

const DODGE_TIME_LIMIT	      = 8.8;
const FLASH_TIME              = 1;

var float DodgeTimeRemaining;
var float DodgeTimeLimit;
var int MaxLives;
var int death;
var const color Black, Gold,Blue,Red, White;
var bool bFlashing;
var bool bReadyFlash;
var bool bShowInfo;
var bool bMoving;
var bool bHideInfo;
var bool bStartPoint;
var bool bInterationStarted;
var bool bGotHit;
var bool bFirstVal;
var bool bShowScore;
var bool bShowPickupDes;
var bool bQuickDodge;
var bool bPlayDeathMusic;
var bool bEasyGame;
var bool bSetHealth;

var array<Font> Commodore;
var vector DyingLoc;
var name Victim;
var name prevVictim;
var int ScoreInterator;
var bool bGetScreenPos;


var const Texture2D MyHudTexture;
var const Texture2D InfoBoardTexture;
var const Texture2D PortraitTexture;
var const Texture2D PortraitBG;
var const Texture2D TestTexture;
var const Texture2D LosingGameTexture;
var const Texture2D WiningGameTexture;
var const Texture2D DodgeBarTexture;
var const Texture2D GoldBarTexture;
var const Texture2D GreenBarTexture;

var array<Texture2D> PickupTexture;
var array<int> PickupDesCounter;
var float CurrentPosY,DesPosY;
var float MyResolutionScaleX, MyResolutionScaleY;
var string BRAnnoucement[6];
var string PrintString;
var string source;
var int Stringiterator;
var int soundindex;
var string ShowInfoMessage;
var int oldHealth,CurrHealth;
var float LastTime;
var int PickupTextureType;

simulated function PostBeginPlay()
{
    local int i;

    super.PostBeginPlay();
    for(i = 0; i < 5; i++)
    {
        PickupDesCounter[i] = 1;
    }

    bEasyGame = BailterRay(WorldInfo.Game).bIsEasy;
}
event PostRender()
{

    super.PostRender();
    MyResolutionScaleX = Canvas.ClipX/1680;
    MyResolutionScaleY = Canvas.ClipY/1050;
    if(UTPawnOwner.bTearOff)
    {
       bSetHealth = false;
    }

}

event Tick(float DeltaTime)
{

}

function DisplayTeamScore(){}

function DisplayDirectionIndicator(byte TeamIndex, vector2D POS, Actor DestActor, float DestScale);

function DisplayMyFragCount()
{
	local int FragCount;
	local UTPlayerReplicationInfo FragPRI;
	local float W, POSX;

	W = 214 * MyResolutionScaleX;


    POSX = Canvas.ClipX * 0.49 - W;

    //POS = ResolveHudPosition(ScoringPosition, 115,44);
	FragPRI = ((PawnOwner != None) && (PawnOwner.PlayerReplicationInfo != None)) ? UTPlayerReplicationInfo(PawnOwner.PlayerReplicationInfo) : UTOwnerPRI;

	Canvas.DrawColor = WhiteColor;

	// Figure out if we should be pulsing

	FragCount = (FragPRI != None) ? FragPRI.Score : 0.0;
	if ( FragCount > LastFragCount )
	{
		FragPulseTime = WorldInfo.TimeSeconds;
		LastFragCount = FragCount;

	}
    //else
    DrawText(POSX - 120* MyResolutionScaleY, 20 * MyResolutionScaleY, "Score:","Commodore");
    DrawGlowText(""$FragCount, POSX + 10 * MyResolutionScaleY, 20 * MyResolutionScaleY, 24 * MyResolutionScaleY, FragPulseTime,true);
}

function DisplayInfoBoard(string Information, optional float StringSize = 0)
{
    local vector2d POS;
    local int startLoc;
    local float X,Y;

    POS = ResolveHudPosition(DollPosition,216, 115);

    if(bStartPoint)
    {
        DesPosY = POS.Y + 85 * MyResolutionScaleY;
        CurrentPosY = POS.Y - 200 * MyResolutionScaleY;
        bStartPoint = false;
    }

    if(CurrentPosY > POS.Y + 85 * MyResolutionScaleY && bShowInfo)
        bMoving = false;

    if(CurrentPosY < POS.Y - 200 * MyResolutionScaleY && !bShowInfo)
    {
        bMoving = false;
        bStartPoint=true;
    }

    Canvas.DrawColor = Blue;
    Canvas.SetPos(POS.X + 300 * MyResolutionScaleX, CurrentPosY );
	Canvas.DrawTile(InfoBoardTexture, 1024 * MyResolutionScaleX, 128 * MyResolutionScaleY, 0, 0, 1024, 128);
	Canvas.DrawColor.A = 255;

    if(!bMoving && bShowInfo)
    {
        source = Information;
        if(StringSize == 0)
        {
           Canvas.TextSize(source, X, Y);
           startLoc = Canvas.ClipX/2.1 - X /2 ;
        }
        else
            startLoc = Canvas.ClipX/2.1 - StringSize / 2 ;
        PrintString = Left(source, Stringiterator);
	    Canvas.DrawColor = White;
	    DrawText(POS.X  + startLoc , CurrentPosY+ 50 * MyResolutionScaleY, ""@PrintString,"Commodore");
    	Canvas.DrawColor.A = 255;
   	}
}

function DrawHUD()
{
    super.DrawHUD();
}

function DrawLivingHud()
{
    DisplayLives();
    DisplayMyFragCount();
	DrawDodgeTime();
	DrawScoreIndicator();
	DisplaySample();

	if(bShowPickupDes)
	{
    	DrawPickupDes();
	}

    if(bShowInfo)
    {
        if(ShowInfoMessage ~= "None")
            ShowInfoMessage =  BRAnnoucement[soundindex];
        DisplayInfoBoard(ShowInfoMessage);
    }

	// Pawn Doll
	if ( bShowDoll && UTPawnOwner != none )
	{
		DisplayPawnDoll();
	}
}

function DisplayLives()
{
    local int CurrentLives;
    local float POSX;
    local BailterRay Game;

    POSX = Canvas.ClipX * 0.51;
    Game = BailterRay(WorldInfo.Game);
    death = UTPlayerOwner.PlayerReplicationInfo.Deaths;
    MaxLives = Game.BailterRayLives;
    CurrentLives = MaxLives  - death - 1;

	Canvas.DrawColor = Gold;
    DrawText(POSX - 90 *  MyResolutionScaleX, 20 * MyResolutionScaleY, "Lives:"@CurrentLives,"Commodore");
    Canvas.DrawColor.A = 255;

}

function DisplaySample()
{
    local int CurrentSamples;
    local float POSX;

    POSX = Canvas.ClipX * 0.51;
    CurrentSamples = BailterRayPlayerController(UTPlayerOwner).CurrentSamples;

    DrawText(POSX + 170 *  MyResolutionScaleX, 20 * MyResolutionScaleY, "Collectables: "$CurrentSamples$"/13","Commodore");
    Canvas.DrawColor.A = 255;
}

function DisplayPawnDoll()
{
	local vector2d POS;
	local color DrawColor, BackColor;
	local float HP;
	local int Health;
	local int MaxHealth ;
	local float ArmorAmount;

	POS = ResolveHudPosition(DollPosition,216, 115);
	Canvas.DrawColor = WhiteColor;

	// should doll be visible?
 	ArmorAmount = UTPawnOwner.ShieldBeltArmor + UTPawnOwner.VestArmor + UTPawnOwner.HelmetArmor + UTPawnOwner.ThighpadArmor;
    DollVisibility = FMax(DollVisibility - 3.0 * (WorldInfo.TimeSeconds - LastDollUpdate), 0.0);

	LastDollUpdate = WorldInfo.TimeSeconds;

	POS.X = POS.X + (DollVisibility - 1.0) * HealthOffsetX * MyResolutionScaleY;
	if ( (ArmorAmount > 0) || (UTPawnOwner.JumpbootCharge > 0) )
	{
       DrawText(POS.X  + 150 * MyResolutionScaleX , POS.Y + 80 * MyResolutionScaleY, " Shield", "Commodore");
		     HP = 400 * MyResolutionScaleX * (UTPawnOwner.ShieldBeltArmor / UTPawnOwner.SuperHealthMax) ;
        	 DrawColor = default.Gold;
	         BackColor = default.GrayColor;
             BackColor.A = 255;
	         DrawBarGraph(POS.X + 100 * MyResolutionScaleX, POS.Y + 100 * MyResolutionScaleY, HP, 200.00 * MyResolutionScaleX, 30.00 * MyResolutionScaleY , Canvas, DrawColor, BackColor);
	}

    if(bSetHealth == false)
    {

        if(bEasyGame)
        {
           UTPawnOwner.Health = 400;
           UTPawnOwner.HealthMax = 400;
        }
        else
        {
            UTPawnOwner.Health = 200;
           UTPawnOwner.HealthMax = 200;
        }
        bSetHealth = true;
    }
	Health = UTPawnOwner.Health;
	if(!bFirstVal)
	{
	    oldHealth = Health;
        bFirstVal = true;
    }

	if(Health == oldHealth && !bGotHit)
    	CurrHealth = Health;
	if(Health != oldHealth && !bGotHit)
	{
        CurrHealth = oldHealth;
        bGotHit = true;
    }

    if(bGotHit)
    {
        if(CurrHealth > Health)
            CurrHealth -= 2;
        else if(CurrHealth < Health)
            CurrHealth += 2;
        else if(CurrHealth == Health)
            bGotHit = false;
    }
	MaxHealth = UTPawnOwner.HealthMax ;

	// Draw the Health Icon
    Canvas.DrawColor = Gold;
	DrawText(POS.X  + 170 * MyResolutionScaleX , POS.Y + 20 * MyResolutionScaleY, " Energy", "Commodore");
    Canvas.DrawColor.A = 255;


    Canvas.DrawColor = White;
    HP = 256 * MyResolutionScaleX * (( float(CurrHealth)) / ( float(MaxHealth))) ;
    Canvas.SetPos(POS.X + 100 * MyResolutionScaleX, POS.Y + 50 * MyResolutionScaleY);
	Canvas.DrawTile(TestTexture, HP , 16 * MyResolutionScaleY, 0, 0, HP, 16);
	Canvas.DrawColor.A=255;

	oldHealth = Health;
}

simulated function DrawDodgeTime()
{
    local vector2d POS;
	local float XP;

	POS = ResolveHudPosition(ScoringPosition, 115,44);

    DrawText(POS.X  - 60 * MyResolutionScaleX , POS.Y + 20 * MyResolutionScaleY, "Dodge Time", "Commodore");

    XP = 272 * MyResolutionScaleX * (DodgeTimeRemaining / DodgeTimeLimit );
	if(XP <= 5 * MyResolutionScaleX)
	    bReadyFlash=true;
    if(bReadyFlash)
    {
       if(bFlashing)
       {
          EnableGuageFlash(XP,POS.X - 130 * MyResolutionScaleX, POS.Y + 50 * MyResolutionScaleY);
       }
    }

    if(!bFlashing)
    {
        Canvas.DrawColor = White;
        Canvas.SetPos(POS.X - 130 * MyResolutionScaleX, POS.Y + 50 * MyResolutionScaleY);
        if(!bQuickDodge)
        	Canvas.DrawTile(DodgeBarTexture, XP , 16 * MyResolutionScaleY, 0, 0, XP, 16);
       	else
       	    Canvas.DrawTile(GreenBarTexture, XP , 16 * MyResolutionScaleY, 0, 0, XP, 16);
    	Canvas.DrawColor.A=255;
    }
}

simulated function EnableGuageFlash(float XP,float X, float Y)
{
    Canvas.DrawColor = White;
    Canvas.SetPos(X, Y);
   	Canvas.DrawTile(GoldBarTexture, XP , 16 * MyResolutionScaleY, 0, 0, XP, 16);
   	Canvas.DrawColor.A = 255;
}

simulated function DrawText (float X, float Y, string Text, optional string Font="Commodore", optional string Size, optional const color TextColor = White)
{

      if (MyResolutionScaleX >= 0.856) {
        Canvas.Font = commodore[1];
      } else if (MyResolutionScaleX <= 0.61) {
        Canvas.Font = Commodore[3];
      } else{
        Canvas.Font = Commodore[2];
      }
      if(Size == "Large")
          Canvas.Font = Commodore[0];

        if (MyResolutionScaleX >= 0.856 && Size == "Large") {
        Canvas.Font = commodore[0];
      } else if(MyResolutionScaleX <= 0.61 && Size == "Large") {
        Canvas.Font = Commodore[2];
      } else if(MyResolutionScaleX < 0.856 && MyResolutionScaleX > 0.61 && Size == "Large"){
        Canvas.Font = Commodore[1];
      }
      Canvas.DrawColor = default.Black;
      if (Font == "tiny" || Font == "small" || Font == "medium") Canvas.SetPos (X+1, Y+1); else Canvas.SetPos (X+2, Y+2);
      Canvas.DrawTextClipped (Text, false);
      Canvas.DrawColor = TextColor;
      Canvas.SetPos (X, Y);
      Canvas.DrawTextClipped (Text, false);
}

function DisplayTeamLogos(byte TeamIndex, vector2d POS, optional float DestScale=1.0){}

function BailterRayDisplayHit(vector HitDir, int Damage, class<DamageType> damageType)
{
	local Vector Loc;
	local Rotator Rot;
	local float DirOfHit_L;
	local vector AxisX, AxisY, AxisZ;
	local vector ShotDirection;
	local bool bIsInFront;
	local vector2D	AngularDist;
	local float PositionInQuadrant;
	local float Multiplier;
	local float DamageIntensity;
	local class<UTDamageType> UTDamage;

	if ( (PawnOwner != None) && (PawnOwner.Health > 0) )
	{
		DamageIntensity = PawnOwner.InGodMode() ? 0.5 : (float(Damage)/100.0 + float(Damage)/float(PawnOwner.Health));
	}
	else
	{
		DamageIntensity = FMax(0.2, 0.02*float(Damage));
	}

	if ( damageType.default.bLocationalHit )
	{
		// Figure out the directional based on the victims current view
		PlayerOwner.GetPlayerViewPoint(Loc, Rot);
		GetAxes(Rot, AxisX, AxisY, AxisZ);

		ShotDirection = Normal(HitDir - Loc);
		bIsInFront = GetAngularDistance( AngularDist, ShotDirection, AxisX, AxisY, AxisZ);
		GetAngularDegreesFromRadians(AngularDist);

		Multiplier = 0.26f / 90.f;
		PositionInQuadrant = Abs(AngularDist.X) * Multiplier;

		if( bIsInFront )
		{
			DirOfHit_L = (AngularDist.X > 0) ? PositionInQuadrant : -1*PositionInQuadrant;
		}
		else
		{
			DirOfHit_L = (AngularDist.X > 0) ? 0.52+PositionInQuadrant : 0.52-PositionInQuadrant;
		}

		// Cause a damage indicator to appear
		DirOfHit_L = -1 * DirOfHit_L;

	}

	if (DamageIntensity > 0 && HitEffect != None)
	{
		DamageIntensity = FClamp(DamageIntensity, 0.2, 1.0);
		if ( (PawnOwner == None) || (PawnOwner.Health <= 0) )
		{
			// long effect duration if killed by this damage
			HitEffectFadeTime = PlayerOwner.MinRespawnDelay;
		}
		else
		{
			HitEffectFadeTime = default.HitEffectFadeTime * DamageIntensity;
		}
		HitEffectIntensity = default.HitEffectIntensity * DamageIntensity;
		UTDamage = class<UTDamageType>(DamageType);
		MaxHitEffectColor = (UTDamage != None && UTDamage.default.bOverrideHitEffectColor) ? UTDamage.default.HitEffectColor : default.MaxHitEffectColor;
		HitEffectMaterialInstance.SetScalarParameterValue('HitAmount', HitEffectIntensity);
		HitEffectMaterialInstance.SetVectorParameterValue('HitColor', MaxHitEffectColor);
		HitEffect.bShowInGame = true;
		bFadeOutHitEffect = true;
	}
}

function DrawPostGameHud()
{
	if (WorldInfo.GRI != None
		&& PlayerOwner.PlayerReplicationInfo != None
		&& !PlayerOwner.PlayerReplicationInfo.bOnlySpectator
		&& !PlayerOwner.IsInState('InQueue') )
	{

		death = UTPlayerOwner.PlayerReplicationInfo.Deaths;

		if(death < BailterRay(WorldInfo.Game).BailterRayLives)
		{
            DrawEndGamePic(WiningGameTexture);
            DrawReport();
        }
        else
        {
            DrawEndGamePic(LosingGameTexture);
            DrawReport();

        }
		//DisplayHUDMessage((bWinner ? YouHaveWon : YouHaveLost));
	}

	DisplayConsoleMessages();
}

function DrawScoreIndicator()
{
    local Pawn P;
    local vector ScreenLoc;
    local int ScoreShowing;

    ForEach DynamicActors(class'Pawn', P)
   	{
        if(P.Health <= 0)
        {

            Victim = P.Tag;
            if(Victim != prevVictim)
            {
                bShowScore = false;
                ScoreInterator = 0;
                DyingLoc.X = 0;
                DyingLoc.Y = 0;
                DyingLoc.Z = 0;
            }
            if(!bGetScreenPos)
            {
                DyingLoc = P.Location;
                bGetScreenPos = true;
            }
            bShowScore = true;
            break;
        }
    }

    if(Victim == 'BailterRay_FireTurret' || Victim == 'BailterRay_FireStaticTurret'
        || Victim == 'BailterRay_GooTurret' || Victim == 'BailterRay_GooRotatingTurret'
        || Victim == 'BailterRay_GooBossTurret' || Victim == 'BailterRay_GooAdvancedTurret'
        || Victim == 'BailterRay_GooAdvancedHardTurret')
    {
         if(ScoreInterator > 30)
         {
             bShowScore = false;
             bGetScreenPos = false;
             ScoreInterator = 0;
             DyingLoc.X = 0;
             DyingLoc.Y = 0;
             DyingLoc.Z = 0;
             Victim = '';
         }
         ScreenLoc = Canvas.Project(DyingLoc);
         if(bShowScore)
	        ScoreInterator+=2;
         Canvas.DrawColor = Gold;
         if(Victim == 'BailterRay_GooBossTurret')
             ScoreShowing = 1000;
         else
             ScoreShowing = 100;

         DrawText(ScreenLoc.X, ScreenLoc.Y - ScoreInterator ,"+"@ScoreShowing,"Commodore");
   	     Canvas.DrawColor.A = 255;
    }
    if(Victim == 'EvilRobot' || Victim == 'BlueSadRobot' )
    {
        if(P==none)
        {
            bShowScore = false;
             bGetScreenPos = false;
            ScoreInterator = 0;
            Victim ='';
            DyingLoc.X = 0;
            DyingLoc.Y = 0;
            DyingLoc.Z = 0;
        }

	    ScreenLoc = Canvas.Project(P.Location);
    	if(bShowScore)
	        ScoreInterator+=3;
        Canvas.DrawColor = Gold;
        if(Victim == 'EvilRobot')
            ScoreShowing = BailterRayPawn_EvilRobot(P).value;
        if(Victim == 'BlueSadRobot')
            ScoreShowing = BailterRayPawn_BlueSadRobot(P).value;

        DrawText(ScreenLoc.X, ScreenLoc.Y - ScoreInterator ,"+"@ScoreShowing,"Commodore");
   	    Canvas.DrawColor.A = 255;
   	}
   	prevVictim = Victim;

}

function DrawEndGamePic(Texture2D EndTexture)
{
    local vector2d POS;

    POS = ResolveHudPosition(DollPosition,216, 115);
    Canvas.DrawColor = White;
    Canvas.SetPos(POS.X * MyResolutionScaleX, POS.Y * MyResolutionScaleY);
	Canvas.DrawTile(EndTexture, 2048 * MyResolutionScaleX , 1050 * MyResolutionScaleY , 0, 0, 2048, 1024);
	Canvas.DrawColor.A = 255;
}

function DrawPickupDes()
{
    Canvas.DrawColor = White;
    Canvas.DrawColor.A = 180;
    Canvas.SetPos(Canvas.ClipX/2.3 , Canvas.ClipY / 2.8 );
	Canvas.DrawTile(PickupTexture[PickupTextureType], 256 * MyResolutionScaleX , 256 * MyResolutionScaleY , 0, 0, 256, 256);
	Canvas.DrawColor.A = 255;
}

function DrawReport()
{
    local vector2d POS;
    local BailterRayPlayerController BPC;
    local int ScoreInt;

    foreach LocalPlayerControllers(class'BailterRayPlayerController', BPC)
    {
        if(BPC.IsPlayerOwned())
            break;
    }
    ScoreInt = int(BPC.PlayerReplicationInfo.Score);
    POS = ResolveHudPosition(DollPosition,216, 115);
    if(BPC != none)
    {
        DrawText(POS.X + 500 * MyResolutionScaleX, POS.Y + 400 * MyResolutionScaleY ,"Player Name:        AstroExplorer","Commodore","Large");
        DrawText(POS.X + 500 * MyResolutionScaleX, POS.Y + 440 * MyResolutionScaleY ,"Total Score:        "@ScoreInt,"Commodore","Large");
        DrawText(POS.X + 500 * MyResolutionScaleX, POS.Y + 480 * MyResolutionScaleY ,"Killed Enemies:     "@BPC.PlayerReplicationInfo.Kills,"Commodore","Large");
        DrawText(POS.X + 500 * MyResolutionScaleX, POS.Y + 700 * MyResolutionScaleY ,"Please wait... ","Commodore","Large");
    }
    Canvas.DrawColor.A = 255;
    if(!bPlayDeathMusic)
    {
        //BPC.PlaySound(SoundCue'Test.endingCue');
        bPlayDeathMusic = true;
    }
}


function DisplayHUDMessage(string Message, optional float XOffsetPct = 0.05, optional float YOffsetPct = 0.05)
{
}

simulated function Timer();

defaultproperties
{
      Victim=""
      //added on 21st aug
      Commodore(1)=Font'BailterRayFonts.BR_C64_Med'
      Commodore(2)=Font'BailterRayFonts.BR_C64_Small'
      Commodore(3)=Font'BailterRayFonts.BR_C64_Tiny'
      Commodore(0)=Font'BailterRayFonts.BR_C64_Large'
      bGetScreenPos = false
      DodgeTimeRemaining = 8.8
      DodgeTimeLimit = 8.8
      InfoBoardTexture=Texture2D'BailterRayUIScenes.Textures.infoboard'
      WiningGameTexture=Texture2D'BailterRayUIScenes.Textures.WinEndGame'
      LosingGameTexture=Texture2D'BailterRayUIScenes.Textures.EndGame'
      TestTexture=Texture2D'BailterRayUIScenes.Textures.RedBar'
      DodgeBarTexture=Texture2D'BailterRayUIScenes.Textures.PurpleBar'
      GoldBarTexture=Texture2D'BailterRayUIScenes.Textures.GoldBar'
      GreenBarTexture=Texture2D'BailterRayUIScenes.Textures.GreenBar'
      PickupTexture(0)=Texture2D'BailterRayPickups.Texture.Des_DodgeMore'
      PickupTexture(1)=Texture2D'BailterRayPickups.Texture.Des_firefaster'
      PickupTexture(2)=Texture2D'BailterRayPickups.Texture.Des_ExtraLife'
      PickupTexture(3)=Texture2D'BailterRayPickups.Texture.Des_MoreEnergy'

      PickupTextureType=0
      ScoreboardSceneTemplate=Scoreboard_CTF'BailterRayUIScenes.ModScenes.sbBailterRay'
      MaxLives = 4
      Stringiterator=1
      Black=(B=0,G=0,R=0,A=255)
      Gold=(B=11,G=183,R=255,A=255)
      Blue=(B=100,G=0,R=0,A=100)
      Red=(B=0,G=0,R=255,A =200)
      White=(B=255,G=255,R=255,A =255)
      DollPosition=(X=0.000000,Y=0.000000)
      ScoringPosition=(X=-1.000000,Y=0.000000)
      bReadyFlash=false
      bFlashing=false
      bShowInfo=false
      bMoving=false
      bHideInfo=false
      bStartPoint=true
      bInterationStarted=false
      bGotHit=false;
      bFirstVal=false;
      bShowScore=false;
      bShowPickupDes=false
      bQuickDodge=false
      bSetHealth=false
      death=0
      ShowInfoMessage="None"
      PressFireToBegin=""
}